package com.wust.baby;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wust.database.DatabaseFactory;
import com.wust.util.ConstDefine;

public class BabyInfoDao {
	private Context context;
	private List<BabyInfo>infolist;
	public BabyInfoDao(Context context)
	{
		this.context=context;
	}
	public void insertBabyInfo(BabyInfo info)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getWritableDatabase();
		ContentValues value=new ContentValues();
		value.put("info_weigth", info.getInfo_weigth());
		value.put("info_height", info.getInfo_height());
		value.put("info_date", info.getInfo_date());
		value.put("baby_id", info.getBaby_id());
		value.put("info_bust",info.getInfo_bust());
		value.put("info_circumference", info.getInfo_circumference());
		value.put("info_descripe", info.getDescription());
		db.insert(ConstDefine.DatabaseFactoryUtil.BABY_INFO_TABLE, null, value);
		db.close();
	}
	public List<BabyInfo>getBabyInfoListById(String babyId)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.BABY_INFO_TABLE, new String[]{"info_weigth","info_height","info_bust","info_descripe","info_circumference","info_date","baby_id"}, "baby_id=?", new String[]{babyId}, null, null, "info_date asc");
		infolist=new ArrayList<BabyInfo>();
		while(cursor.moveToNext())
		{
			BabyInfo info=new BabyInfo();
			info.setInfo_weigth(cursor.getDouble(cursor.getColumnIndex("info_weigth")));
			info.setInfo_height(cursor.getDouble(cursor.getColumnIndex("info_height")));
			info.setInfo_date(cursor.getString(cursor.getColumnIndex("info_date")));
			info.setBaby_id(cursor.getInt(cursor.getColumnIndex("baby_id")));
			info.setInfo_bust(cursor.getDouble(cursor.getColumnIndex("info_bust")));
			info.setInfo_circumference(cursor.getDouble(cursor.getColumnIndex("info_circumference")));
			info.setDescription(cursor.getString(cursor.getColumnIndex("info_descripe")));
			infolist.add(info);
		}
		cursor.close();
		db.close();
		return infolist;
	}
	public BabyInfo getBabyInfoById(String babyId,String time)
	{
		System.out.println("time:"+time+"id"+babyId);
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.BABY_INFO_TABLE, new String[]{"info_weigth","info_height","info_descripe","info_bust","info_circumference","info_date","baby_id"}, "baby_id=? and info_date='"+time+"'", new String[]{babyId}, null, null, "info_date asc");
		BabyInfo info=new BabyInfo();
		while(cursor.moveToNext())
		{	
			info.setInfo_weigth(cursor.getDouble(cursor.getColumnIndex("info_weigth")));
			info.setInfo_height(cursor.getDouble(cursor.getColumnIndex("info_height")));
			info.setInfo_date(cursor.getString(cursor.getColumnIndex("info_date")));
			info.setInfo_bust(cursor.getDouble(cursor.getColumnIndex("info_bust")));
			info.setInfo_circumference(cursor.getDouble(cursor.getColumnIndex("info_circumference")));
			info.setBaby_id(cursor.getInt(cursor.getColumnIndex("baby_id")));
			info.setDescription(cursor.getString(cursor.getColumnIndex("info_descripe")));
			
		}
		cursor.close();
		db.close();
		return info;
	}
}
